package dyy.zc.campuspay.ui.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.regex.Pattern;

import cn.bmob.v3.datatype.BmobFile;
import dyy.zc.campuspay.R;
import dyy.zc.campuspay.application.MyApplication;
import dyy.zc.campuspay.base.BaseActivity;
import dyy.zc.campuspay.utils.BmobUtils;
import dyy.zc.campuspay.utils.ToastUtils;

public class ZhifuPasswordActivity extends BaseActivity {

    private TextView title,right;
    private EditText password,zhifumimaPre,zhifumimaConfirm;
    private String st_password,st_zhifumima,st_zhifumimaConfirm;
    private BmobUtils bmobUtils;
    public Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 50){
                dialog.dismiss();
                ToastUtils.shortToast(ZhifuPasswordActivity.this, "支付密码设置成功，请牢记");
                Intent intent = new Intent();
                intent.putExtra("mima",true);
                setResult(555,intent);
                finish();
            }
        }
    };
    @Override
    protected void findViewByIDS() {
        title = findViewsById(R.id.top_tv_title);
        right = findViewsById(R.id.top_tv_right);
        right.setVisibility(View.VISIBLE);
        right.setText("保存");
        title.setText("设置支付密码");
        password = findViewsById(R.id.tv_password);
        zhifumimaPre = findViewsById(R.id.tv_zhifumimaPre);
        zhifumimaConfirm = findViewsById(R.id.tv_zhifumima_confirm);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhifu_password);
        bind();
    }

    private void bind() {
        bmobUtils = new BmobUtils(this);
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                st_password = password.getText().toString();
                st_zhifumima = zhifumimaPre.getText().toString();
                st_zhifumimaConfirm = zhifumimaConfirm.getText().toString();
                if (!st_password.equals("") || !st_zhifumima.equals("") || !st_zhifumimaConfirm.equals("")) {
                    if (st_password.equals(loginuser.getPassword())) {

                        if (check()) {
                            if (st_zhifumima.equals(st_zhifumimaConfirm)) {
                                dialog = ProgressDialog.show(ZhifuPasswordActivity.this, null, "正在设置...");
                                loginuser.setZhifuPassword(st_zhifumima);
                                MyApplication.getInstance().saveUserInfo(loginuser);
                                bmobUtils.updateAccount(loginuser);
                            } else {
                                ToastUtils.shortToast(ZhifuPasswordActivity.this, "两次输入的密码不一致");
                            }
                        } else {
                            ToastUtils.shortToast(ZhifuPasswordActivity.this, "请输入6位支付密码");
                        }
                    } else {
                        ToastUtils.shortToast(ZhifuPasswordActivity.this, "原密码输入错误");
                    }
                } else {
                    ToastUtils.shortToast(ZhifuPasswordActivity.this, "请输入密码");
                }
            }
        });
    }

    /**
     * 验证支付密码为6位
     *
     * @return
     */
    public boolean check() {
        //验证密码（6-16数字字母）
        if (st_zhifumima.length() ==6) {
            return true;
        }
        return false;
    }
}
